﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bingosoft.Web.Utils;
using HHMMS.UI.Common;
using HHMMS.UI.Common.Models;
using System.Data;

namespace HHMMS.UI.Modules.Common
{
    public partial class AttachmentInfoList : ListPage
    {
        private readonly AttachmentService _attachmentService = ObjectHelper.GetObject<AttachmentService>();        

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void lbtnRefresh_Click(object sender, EventArgs e)
        {
            this.GridSearch.Search();
        }

        protected void SearchGrid(object sender, EventArgs e)
        {
            this.GridSearch.Search();
        }

        protected void RefreshGrid(object sender, EventArgs e)
        {
            this.GridSearch.Refresh();
        }

        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var lbFileSize = e.Row.FindControl<Label>("lblFileSize");
                var dataRow = e.Row.DataItem as DataRowView;
                if (dataRow != null && dataRow["FileSize"] != DBNull.Value)
                {
                    var fileSize = Convert.ToDecimal(dataRow["FileSize"]);
                    var size = fileSize / 1024;
                    if (size > 1024)
                    {
                        lbFileSize.Text = (size / 1024).ToString("0.00") + "MB";
                    }
                    else
                    {
                        lbFileSize.Text = size.ToString("0.00") + "KB";
                    }
                }
            }
        }

        protected void DeleteAttachmentInfo(object sender, EventArgs e)
        {
            ImageButton ctr = sender as ImageButton;
            String id = ctr.Attributes["_ID"];

            this._attachmentService.DeleteAttachmentInfo(id);

            this.GridSearch.Search();
        }

    }
}